{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "crazy-height",
   "metadata": {},
   "outputs": [],
   "source": [
    "from model.resnet_sgd_cosineannealing_inference import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "micro-testament",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define input image dimensions for resizing\n",
    "height = 448\n",
    "width = 448\n",
    "\n",
    "# Define model hyperparameters\n",
    "lr = 0.01\n",
    "momentum = 0.9\n",
    "T_0 = 225 # e.g. 899 / 4 (train_dataset_size / batch_size)\n",
    "T_mult = 1\n",
    "epochs = 20\n",
    "batch_size = 4 # For both train and test sets\n",
    "\n",
    "# Define number of layers for the ResNet neural network, select from [18, 34, 50 ,101, 152]\n",
    "num_layers = 50\n",
    "\n",
    "pretrained_weights = True\n",
    "unfreeze_all_layers = 'False' # i.e. Default: 'False', unfreezes last layer only for tuning\n",
    "\n",
    "train_augmentation = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "likely-limit",
   "metadata": {},
   "outputs": [],
   "source": [
    "bucket = None\n",
    "saved_model_path = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "english-reynolds",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = \"training_data/dogs/spaniel_breeds\"\n",
    "model_dir = \"models\"\n",
    "\n",
    "class _args:\n",
    "    image_height = height\n",
    "    image_width = width\n",
    "    train = os.path.join(data_dir, \"train\")\n",
    "    validation = os.path.join(data_dir, \"validation\")\n",
    "    test = os.path.join(data_dir, \"test\")\n",
    "    model_dir = model_dir\n",
    "    batch_size = batch_size\n",
    "    epochs = epochs\n",
    "    lr = lr\n",
    "    momentum = momentum\n",
    "    T_0 = T_0\n",
    "    T_mult = T_mult\n",
    "    num_layers = num_layers\n",
    "    pretrained_weights = pretrained_weights\n",
    "    s3_bucket = bucket\n",
    "    warm_restart = saved_model_path\n",
    "    unfreeze_all_layers = unfreeze_all_layers\n",
    "    train_augmentation = train_augmentation\n",
    "args = _args()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "excellent-queens",
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets = create_datasets(args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "flying-shakespeare",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/20\n",
      "----------\n",
      "train Loss: 2.0704; train Acc: 0.5460;\n",
      "validation Loss: 0.2478; validation Acc: 0.9333;\n",
      "epoch: 1; lr: 0.0012150247217412185;\n",
      "\n",
      "Epoch 2/20\n",
      "----------\n",
      "train Loss: 1.8848; train Acc: 0.6638;\n",
      "validation Loss: 0.2982; validation Acc: 0.9167;\n",
      "epoch: 2; lr: 0.004269584857187943;\n",
      "\n",
      "Epoch 3/20\n",
      "----------\n",
      "train Loss: 2.1504; train Acc: 0.6839;\n",
      "validation Loss: 0.8172; validation Acc: 0.8333;\n",
      "epoch: 3; lr: 0.007679133974894983;\n",
      "\n",
      "Epoch 4/20\n",
      "----------\n",
      "train Loss: 1.1098; train Acc: 0.7615;\n",
      "validation Loss: 0.6214; validation Acc: 0.8500;\n",
      "epoch: 4; lr: 0.009786597487660335;\n",
      "\n",
      "Epoch 5/20\n",
      "----------\n",
      "train Loss: 1.5574; train Acc: 0.7141;\n",
      "validation Loss: 0.3282; validation Acc: 0.9333;\n",
      "epoch: 5; lr: 0.00043227271178699624;\n",
      "\n",
      "Epoch 6/20\n",
      "----------\n",
      "train Loss: 1.7752; train Acc: 0.6853;\n",
      "validation Loss: 0.7003; validation Acc: 0.9167;\n",
      "epoch: 6; lr: 0.0028711035421746366;\n",
      "\n",
      "Epoch 7/20\n",
      "----------\n",
      "train Loss: 1.2992; train Acc: 0.7874;\n",
      "validation Loss: 1.7819; validation Acc: 0.8333;\n",
      "epoch: 7; lr: 0.00634459910307633;\n",
      "\n",
      "Epoch 8/20\n",
      "----------\n",
      "train Loss: 1.0730; train Acc: 0.7974;\n",
      "validation Loss: 1.2846; validation Acc: 0.8667;\n",
      "epoch: 8; lr: 0.009164606203550498;\n",
      "\n",
      "Epoch 9/20\n",
      "----------\n",
      "train Loss: 1.3102; train Acc: 0.7830;\n",
      "validation Loss: 0.4053; validation Acc: 0.9500;\n",
      "epoch: 9; lr: 3.9426493427611173e-05;\n",
      "\n",
      "Epoch 10/20\n",
      "----------\n",
      "train Loss: 1.0107; train Acc: 0.7974;\n",
      "validation Loss: 0.4734; validation Acc: 0.9167;\n",
      "epoch: 10; lr: 0.0016543469682057106;\n",
      "\n",
      "Epoch 11/20\n",
      "----------\n",
      "train Loss: 0.8286; train Acc: 0.8175;\n",
      "validation Loss: 1.0473; validation Acc: 0.8667;\n",
      "epoch: 11; lr: 0.004895287900583216;\n",
      "\n",
      "Epoch 12/20\n",
      "----------\n",
      "train Loss: 0.7524; train Acc: 0.8319;\n",
      "validation Loss: 0.6312; validation Acc: 0.8500;\n",
      "epoch: 12; lr: 0.008187119948743448;\n",
      "\n",
      "Epoch 13/20\n",
      "----------\n",
      "train Loss: 0.7490; train Acc: 0.8376;\n",
      "validation Loss: 0.5544; validation Acc: 0.9000;\n",
      "epoch: 13; lr: 0.009929980185352525;\n",
      "\n",
      "Epoch 14/20\n",
      "----------\n",
      "train Loss: 1.5125; train Acc: 0.7529;\n",
      "validation Loss: 0.5882; validation Acc: 0.9167;\n",
      "epoch: 14; lr: 0.0007231786991974681;\n",
      "\n",
      "Epoch 15/20\n",
      "----------\n",
      "train Loss: 1.5376; train Acc: 0.7615;\n",
      "validation Loss: 0.5755; validation Acc: 0.9167;\n",
      "epoch: 15; lr: 0.003454915028125263;\n",
      "\n",
      "Epoch 16/20\n",
      "----------\n",
      "train Loss: 0.9735; train Acc: 0.8276;\n",
      "validation Loss: 0.3446; validation Acc: 0.9000;\n",
      "epoch: 16; lr: 0.006937577932260515;\n",
      "\n",
      "Epoch 17/20\n",
      "----------\n",
      "train Loss: 1.1845; train Acc: 0.7917;\n",
      "validation Loss: 1.0001; validation Acc: 0.7833;\n",
      "epoch: 17; lr: 0.009478558801197065;\n",
      "\n",
      "Epoch 18/20\n",
      "----------\n",
      "train Loss: 1.1128; train Acc: 0.8247;\n",
      "validation Loss: 0.4447; validation Acc: 0.9333;\n",
      "epoch: 18; lr: 0.00015708419435684517;\n",
      "\n",
      "Epoch 19/20\n",
      "----------\n",
      "train Loss: 1.0153; train Acc: 0.8161;\n",
      "validation Loss: 0.6012; validation Acc: 0.9167;\n",
      "epoch: 19; lr: 0.002146432161577842;\n",
      "\n",
      "Epoch 20/20\n",
      "----------\n",
      "train Loss: 1.0746; train Acc: 0.8075;\n",
      "validation Loss: 0.4985; validation Acc: 0.9167;\n",
      "epoch: 20; lr: 0.0055226423163382676;\n",
      "\n",
      "Training complete in 230m 52s\n",
      "Best validation Acc: 0.950000\n",
      "models\\model.pth\n",
      "\n",
      "Evaluating best weights:\n",
      "--------------------\n",
      "train Loss: 0.2231 Acc: 0.9511\n",
      "train Avg. F1 Score: 0.951;\n",
      "classification_report: \n",
      "                  precision    recall  f1-score   support\n",
      "\n",
      "American Spaniel       0.89      0.84      0.87        89\n",
      "        Blenheim       0.94      0.98      0.96       113\n",
      "         Clumber       1.00      1.00      1.00       121\n",
      "          Cocker       0.98      0.97      0.98       130\n",
      "   Irish Spaniel       0.89      0.94      0.91       119\n",
      "Japanese Spaniel       0.98      0.94      0.96       124\n",
      "\n",
      "        accuracy                           0.95       696\n",
      "       macro avg       0.95      0.95      0.95       696\n",
      "    weighted avg       0.95      0.95      0.95       696\n",
      ";\n",
      "\n",
      "validation Loss: 0.4053 Acc: 0.9500\n",
      "validation Avg. F1 Score: 0.950;\n",
      "classification_report: \n",
      "                  precision    recall  f1-score   support\n",
      "\n",
      "American Spaniel       0.89      0.80      0.84        10\n",
      "        Blenheim       1.00      1.00      1.00        10\n",
      "         Clumber       1.00      1.00      1.00        10\n",
      "          Cocker       1.00      0.90      0.95        10\n",
      "   Irish Spaniel       0.83      1.00      0.91        10\n",
      "Japanese Spaniel       1.00      1.00      1.00        10\n",
      "\n",
      "        accuracy                           0.95        60\n",
      "       macro avg       0.95      0.95      0.95        60\n",
      "    weighted avg       0.95      0.95      0.95        60\n",
      ";\n",
      "\n",
      "test Loss: 0.2199 Acc: 0.9333\n",
      "test Avg. F1 Score: 0.933;\n",
      "classification_report: \n",
      "                  precision    recall  f1-score   support\n",
      "\n",
      "American Spaniel       0.89      0.80      0.84        10\n",
      "        Blenheim       0.91      1.00      0.95        10\n",
      "         Clumber       1.00      1.00      1.00        10\n",
      "          Cocker       1.00      0.90      0.95        10\n",
      "   Irish Spaniel       0.83      1.00      0.91        10\n",
      "Japanese Spaniel       1.00      0.90      0.95        10\n",
      "\n",
      "        accuracy                           0.93        60\n",
      "       macro avg       0.94      0.93      0.93        60\n",
      "    weighted avg       0.94      0.93      0.93        60\n",
      ";\n",
      "\n"
     ]
    }
   ],
   "source": [
    "train(args, datasets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "english-appreciation",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
